package es.udima.geo.geoserver.entities;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * <p>
 * Entidad que representa un usuario del sistema.
 * </p>
 * 
 * @author david
 */
@Entity
@Table(name = "users")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Identificador interno de usuario
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;

    /**
     * Nombre de usuario
     */
    private String userName;

    /**
     * Clave del usuario
     */
    private String password;

    /**
     * Nueva clave del usuario. Utilizado solo en caso de realizar un cambio de
     * clave o creación de usuario.
     */
    @Transient
    private String newPassword;

    /**
     * Repetición para validación de la nueva clave del usuario. Utilizado solo
     * en caso de realizar un cambio de clave o creación de usuario.
     */
    @Transient
    private String newPasswordRepeated;

    /**
     * Nombre del usuario.
     */
    private String firstName;

    /**
     * Apellidos del usuario.
     */
    private String lastName;

    /**
     * Email del usuario.
     */
    private String email;

    /**
     * Repetición del email de usuario. Utilizado para validación sólo en caso
     * de creación de usuario.
     */
    @Transient
    private String emailRepeated;

    /**
     * Tipo de vehículo del usuario.
     */
    @Enumerated(EnumType.STRING)
    private VehicleType vehicleType;

    /**
     * Intervalo de tiempo en que se recuperan los datos gps de este usuario.
     */
    private Long fetchGpsLapseTime;

    /**
     * Indica si el usuario está activo.
     */
    private boolean active;

    /**
     * Indica si el usuario está conectado.
     */
    @Transient
    private boolean connected;

    /**
     * Constructor
     */
    public User() {
    }

    public String getId() {
	return id;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getUserName() {
	return userName;
    }

    public void setUserName(String userName) {
	this.userName = userName;
    }

    public String getPassword() {
	return password;
    }

    public void setPassword(String password) {
	this.password = password;
    }

    public String getFirstName() {
	return firstName;
    }

    public void setFirstName(String firstName) {
	this.firstName = firstName;
    }

    public String getLastName() {
	return lastName;
    }

    public void setLastName(String lastName) {
	this.lastName = lastName;
    }

    public String getEmail() {
	return email;
    }

    public void setEmail(String email) {
	this.email = email;
    }

    public VehicleType getVehicleType() {
	return vehicleType;
    }

    public void setVehicleType(VehicleType vehicleType) {
	this.vehicleType = vehicleType;
    }

    public void setVehicleTypeStr(String vehicleType) {
	try {
	    this.vehicleType = VehicleType.valueOf(vehicleType);
	} catch (IllegalArgumentException e) {
	    this.vehicleType = null;
	}
    }

    public boolean isConnected() {
	return connected;
    }

    public void setConnected(boolean connected) {
	this.connected = connected;
    }

    public Long getFetchGpsLapseTime() {
	return fetchGpsLapseTime;
    }

    public void setFetchGpsLapseTime(Long fetchGpsLapseTime) {
	this.fetchGpsLapseTime = fetchGpsLapseTime;
    }

    public void setFetchGpsLapseTimeStr(String fetchGpsLapseTime) {
	if (fetchGpsLapseTime.isEmpty()) {
	    this.fetchGpsLapseTime = null;
	} else {
	    try {
		this.fetchGpsLapseTime = Long.parseLong(fetchGpsLapseTime);
	    } catch (NumberFormatException e) {
		this.fetchGpsLapseTime = Long.MIN_VALUE;
	    }
	}
    }

    public boolean isActive() {
	return active;
    }

    public void setActive(boolean active) {
	this.active = active;
    }

    public String getNewPassword() {
	return newPassword;
    }

    public void setNewPassword(String newPassword) {
	this.newPassword = newPassword;
    }

    public String getNewPasswordRepeated() {
	return newPasswordRepeated;
    }

    public void setNewPasswordRepeated(String newPasswordRepeated) {
	this.newPasswordRepeated = newPasswordRepeated;
    }

    public String getEmailRepeated() {
	return emailRepeated;
    }

    public void setEmailRepeated(String emailRepeated) {
	this.emailRepeated = emailRepeated;
    }

    @Override
    public String toString() {
	return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", newPassword=" + newPassword + ", newPasswordRepeated="
		+ newPasswordRepeated + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", emailRepeated="
		+ emailRepeated + ", vehicleType=" + vehicleType + ", fetchGpsLapseTime=" + fetchGpsLapseTime + ", active=" + active + ", connected="
		+ connected + "]";
    }

}
